package com.util.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Enumeration;

/**
 * 参数封装
 */
public class ParameController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.service(req, resp);

    }

    /**
     * 参数封装方法
     * @param request
     * @param method
     * @param c
     */
    public void parameter(HttpServletRequest  request,String method,Class c ) throws Exception{
        Method[] methods = c.getMethods();
        Method m = null;
        for (Method method1 : methods) {
            if (method.equals(method1.getName())) {
                m = method1;
            }
        }
        if (m == null) {
            throw new RuntimeException(String.format("%s方法不存在", method));
        }
        Parameter[] parameters = m.getParameters();
        Enumeration enumerations =  request.getHeaderNames();

        m.invoke(c,enumerations);
    }}